﻿/**
* 该类为皮肤加载管理类
* @author lbynet
* @version 0.1
*/

package com.lbynet.net{

	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class SkinManager extends EventDispatcher {
		public static  const SKINCOMPLETE:String="skincomplete";
		public var skinPath:String;
		public var loader:Loader;

		public function SkinManager() {
			if (instance != null) {
				throw new Error("不能直接创建对象");
			}
			loader=new Loader();
		}
		private static  var instance:SkinManager=new SkinManager();

		public static function getInstance():SkinManager {
			return instance;
		}
		public function get Path():String {
			return this.skinPath;
		}
		public function loadSkin(path:String):void {
			this.skinPath=path;

			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			loader.load(new URLRequest(this.skinPath));
		}
		private function completeHandler(e:Event) {
			//移除侦听器
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			dispatchEvent(new Event(SkinManager.SKINCOMPLETE));//发布事件
		}
		public function getClass(className:String):Class {
			try {
				return loader.contentLoaderInfo.applicationDomain.getDefinition(className)  as  Class;
			} catch (e:Error) {
				throw new Error(className + " definition not found in " + e.toString());
			}
			return null;
		}
	}
}